#include "includes.h"
#include "CBlast.h"

std::unique_ptr<Gdiplus::Image> CBlast::_Image[5];

CBlast::CBlast()
{
}

CBlast::~CBlast()
{
}

VOID CBlast::InitCBlast() {
	WCHAR filename[MAX_PATH];

	for (INT frame = 0; frame < 5; ++frame) {
		wsprintfW(filename, L"res/images/blast/%d.gif", frame);
		auto it = std::make_unique<Gdiplus::Image>(filename);
		if (it->GetLastStatus() != Gdiplus::Ok) {
			throw std::runtime_error(wstring_to_string(filename));
		}
		_Image[frame] = std::move(it);
	}
}

VOID CBlast::Set(INT X, INT Y) {
	_Pos = { X, Y };
	_Run = TRUE;
	_Idx = 0;
}

VOID CBlast::Logic() {
	if (!_Run) {
		return;
	}
	static INT frame[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0 };
	_Frame = frame[_Idx];
	if (_Cnt++ > 40) {
		_Cnt = 0;
		++_Idx;
	}
	if (_Idx >= 16) {
		_Run = FALSE;
	}
}

VOID CBlast::Draw(Gdiplus::Graphics& graphics) {
	if (!_Run) {
		return;
	}
	auto image = _Image[_Frame].get();
	graphics.DrawImage(image, _Pos.X, _Pos.Y, Box32, Box32);
}
